// #ifdef APP-NVUE
const BindingX = uni.requireNativePlugin('bindingx');
const dom = uni.requireNativePlugin('dom');
const animation = uni.requireNativePlugin('animation');

export default {
	data() {
		return {
            delOpen:false,
            platform:null
        }
	},
	watch: {
		show(newVal) {
			// if (this.autoClose) return
			if (this.stop) return
			this.stop = true
			if (newVal) {
				this.open(newVal)
			} else {
				this.close()
			}
		},
		buttons(newVal) {
			this.init()
		}
	},
	created() {
		this.group = this.getParent()
		if (this.group.children !== undefined) {
			this.group.children.push(this)
		}
	},

	mounted() {
        //uni.$on('closeAll',this.closeAll)
		this.$nextTick(()=>{
			this.box = this.getEl(this.$refs['fui_swipea_wrap'])
			this.selector = this.getEl(this.$refs['fui_swipea_content']);
			this.rightButton = this.getEl(this.$refs['fui_swipea_buttons']);
			this.rightButton1 = this.getEl(this.$refs['button1']);
			this.rightButton2 = this.getEl(this.$refs['button2']);
			this.rightButton3 = this.getEl(this.$refs['button3']);
		})
		this.init()
	},
    unmounted(){
        //uni.$off('closeAll',this.closeAll)
    },

	methods: {
		init() {


			this.$nextTick(() => {
				this.x = 0
				this.button = {
					show: false
				}
				setTimeout(() => {
                    this.button['right'] = {width:uni.upx2px(481)};
                    this.open(this.show)
					//this.getSelectorQuery()
				}, 200)
			})
		},
		handleClick(e, index, item) {
			e.stopPropagation();
			this.$emit('click', {
				item,
				index,
				param: this.param
			})
		},
		touchstart(e) {
			if (this.disabled) return
			// 每次只触发一次，避免多次监听造成闪烁
			if (this.stop) return
			this.stop = true
			if (this.autoClose) {
				this.group && this.group.closeAuto(this)
			}




			const rightWidth = this.button.right.width || 0;
            if(this.delOpen){
                this.move2(this.rightButton3,rightWidth/3);
            }


			let expression = this.range(this.x, -rightWidth, 0)
			let rightExpression = this.range(this.x + rightWidth, 0, rightWidth)
			let rightExpressio2 = this.range2(-this.x, 0, rightWidth)
			let rightExpressio3 = this.range3(-this.x, 0, rightWidth)

			this.eventpan = BindingX.bind({
				anchor: this.box,
				eventType: 'pan',
				props: [{
					element: this.selector,
					property: 'transform.translateX',
					expression
				}, {
					element: this.rightButton,
					property: 'transform.translateX',
					expression: rightExpression
				},{
					element: this.rightButton2,
					property: 'transform.translateX',
					expression: rightExpressio2
				},
                {
					element: this.rightButton3,
					property: 'transform.translateX',
					expression: rightExpressio3
				}
            
            ]
			}, (e) => {


				if (e.state === 'end') {
                    if(e.deltaX<0&&this.isopen){
                        this.open(true)
                        this.isclick = true;
                        setTimeout(()=>{
                            this.isclick = false;
                        },500)
                        return;
                    }else{
                        this.delOpen = false;
                    }
					this.x = e.deltaX + this.x;
					this.bindTiming(e.deltaX)
				}
			});
		},
		touchend(e) {
			// if (this.isopen && !this.isclick) {
			// 	this.open(false)
			// }
		},
		bindTiming(x) {

            // if(this.x<0&&x<0){
            //     return;
            // }

			const left = this.x
			const rightWidth = this.button.right.width || 0
			const threshold = Number(this.threshold)
			if (!this.isopen) {
				if (left < -threshold) {
					this.open(true)
				} else {
					this.open(false)
				}
			} else {
				if ((x < threshold && x > 0) || (x < -threshold)) {
					this.open(true)
				} else {
					this.open(false)
				}
			}
		},
		range(num, mix, max) {
			return `min(max(x+${num}, ${mix}), ${max})`
		},
        range2(num, mix, max) {
			return `min(max(${num}-x, ${mix}), ${max})/3`
		},
		range3(num, mix, max) {
			return `min(max(${num}-x, ${mix}), ${max})/3*2`
		},
		open(type) {

            if (this.autoClose) {
				this.group && this.group.closeAuto(this)
			}

            this.$emit('change', {
                isOpen: type,
                delOpen:false,
                param: this.param
            })
			this.animation(type)
		},
		close() {
			this.animation(false)
		},
		closeAll() {
			this.open(false)
		},
		animation(type) {
			const time = 300
			const rightWidth = this.button.right.width || 0
			if (this.eventpan && this.eventpan.token) {
				BindingX.unbind({
					token: this.eventpan.token,
					eventType: 'pan'
				})
			}

			if (type) {
				Promise.all([
					this.move(this.selector, -rightWidth),
					this.move(this.rightButton, 0),
                    this.move(this.rightButton2, rightWidth/3),
					this.move(this.rightButton3, rightWidth/3*2),
				]).then(() => {
					this.setEmit(-rightWidth, type)
				})
			} else {
				Promise.all([
					this.move(this.selector, 0),
					this.move(this.rightButton, rightWidth),
                    this.move(this.rightButton2, 0),
					this.move(this.rightButton3, 0),
				]).then(() => {
					this.setEmit(0, type)
				})
			}
		},
		setEmit(x, type) {
			const rightWidth = this.button.right.width
			this.isopen = this.isopen || false
			this.stop = false
			this.isclick = false
			if (this.isopen !== type && this.x !== x) {
				// if (type && rightWidth > 0) {
				// 	this.$emit('change', {
				// 		isOpen: true,
                //         delOpen:false,
				// 		param: this.param
				// 	})
				// }
				// if (!type) {
				// 	this.$emit('change', {
				// 		isOpen: false,
                //         delOpen:false,
				// 		param: this.param
				// 	})
				// }
			}



			this.x = x
			this.isopen = type;
		},

        delChange(){
            this.delOpen = true;
            const rightWidth = this.button.right.width
            this.move2(this.rightButton3,rightWidth);
            if(uni.platform==='ios'){
                setTimeout(()=>{
                    this.move1(this.rightButton3,0);
                },50)
            }
            

            this.$emit('change', {
                isOpen:true,
                delOpen:true,
                param: this.param
            })
        },
		move(ref, value) {
			return new Promise((resolve, reject) => {
				animation.transition(ref, {
					styles: {
						transform: `translateX(${value})`,
					},
					duration: 150, //ms
					timingFunction: 'linear',
					needLayout: false,
					delay: 0 //ms
				}, function(res) {
					resolve(res)
				})
			})
		},

        move1(ref, value) {
			return new Promise((resolve, reject) => {
				animation.transition(ref, {
					styles: {
						transform: `translateX(${value})`,
					},
					duration: 150, //ms
					timingFunction: 'linear',
					needLayout: false,
					delay: 0 //ms
				}, function(res) {
					resolve(res)
				})
			})
		},
        move2(ref, width) {
			return new Promise((resolve, reject) => {
				animation.transition(ref, {
					styles: {
                        width:`${width}px`
					},
					duration: uni.platform ==='android'?0:150, //ms
					timingFunction: 'linear',
					needLayout: true,
					delay: 0 //ms
				}, function(res) {
					resolve(res)
				})
			})
		},

		getEl(el) {
			return el.ref
		},
		getSelectorQuery() {
			Promise.all([
				this.getDom(),
			]).then((data) => {
				// let show = false
				// if (!this.autoClose) {
				// 	show = this.show
				// }
				// if (show) {
				// 	this.open(show)
				// }
				this.open(this.show)
			})

		},
		getDom() {
			return new Promise((resolve, reject) => {
				dom.getComponentRect(this.$refs['fui_swipea_buttons'], (data) => {
					if (data) {

                        console.log('dddddddddddddddddd')
                        console.log('dddddddddddddddddd')
                        console.log('dddddddddddddddddd')
                        console.log(data.size)

						this.button['right'] = data.size
						resolve(data)
					} else {
						reject()
					}
				})
			})
		}
	}
}

// #endif

// #ifndef APP-NVUE
export default {}
// #endif
